package info.tsukaworld.gui;

import info.tsukaworld.actor.Player;
import java.awt.event.*;

public class GameKeyListener implements KeyListener
{
    private Player player;
    private char[] inputHistory = new char[2];
    GameKeyListener(Player p)
    {
        player = p;
        player.setInput( this );
        inputHistory[0] = 0;
        inputHistory[1] = 0;
    }

    public void keyPressed(KeyEvent e) 
    {
        //System.out.println("ADDING KEY " + (char)e.getKeyCode());
        //displayInput();
        add( (char)e.getKeyCode() );
        //System.out.println("NEW HISTORY");
        //displayInput();
    }
    
    public void displayInput()
    {
        System.out.println("Input[0] = " + inputHistory[0] + " Input[1] = " + inputHistory[1]);
    }
    
    public void add(char move)
    {
        if( inputHistory[0] != move && inputHistory[1] != move )
        {
            if( inputHistory[0] == 0 && inputHistory[1] == 0 )
            {
                inputHistory[1] = move;
            }
            else if( inputHistory[0] == 0 )
            {
                inputHistory[0] = move;
            }
            else if( inputHistory[1] == 0 )
            {
                inputHistory[1] = inputHistory[0];
                inputHistory[0] = move;
            }
            else if( !(inputHistory[0] == 0 && inputHistory[1] == 0))
            {
                inputHistory[1] = inputHistory[0];
                inputHistory[0] = move;
            }
            else
            {
                //System.out.println("ERROR IN KEY ADDITION");
            }
        }
    }

    public void remove(char move)
    {
        if( inputHistory[0] == move)
        {
            inputHistory[0] = 0;
        }
        else if( inputHistory[1] == move )
        {
            inputHistory[1] = 0;
        }
        //else
            //System.out.println("ERROR IN KEY REMOVAL");
    }


    public void keyReleased(KeyEvent e)
    {
        //System.out.println("REMOVING KEY " + (char)e.getKeyCode());
        //displayInput();
        remove( (char)e.getKeyCode() );
        //System.out.println("NEW HISTORY");
        //displayInput();
    }

    public void checkInput()
    {
        if( !(player.isMoving()) )
        {
            switch (inputHistory[0] == 0 ? inputHistory[1] : inputHistory[0]) 
            {
                case KeyEvent.VK_UP :
                {
                    player.setDirection( 0 );
                    player.startMove();
                    break;
                }   
                case KeyEvent.VK_RIGHT : 
                {
                    player.setDirection( 90 );
                    player.startMove();
                    break;
                }  
                case KeyEvent.VK_DOWN : 
                {
                    player.setDirection( 180 );
                    player.startMove();
                    break;
                }   
                case KeyEvent.VK_LEFT : 
                {
                    player.setDirection( 270 );
                    player.startMove();

                    break;
                }   
            }
        }
    }

    public char[] getKeyHistory()
    { return inputHistory; }

    public void keyTyped(KeyEvent e)
    {}
}